package DAO;

import java.util.ArrayList;
import java.util.Collection;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


import TO.EdicaoTO;


public class EdicaoDAO {
	
	private static SessionFactory fabrica;
	
	public EdicaoDAO(){
		fabrica = new Configuration().configure("TO/hibernate.cfg.xml").buildSessionFactory();
	}
	
	public boolean inserir(EdicaoTO edicao) {
			
		try {
			Session sessao = fabrica.openSession();
						
			Transaction transaction = sessao.beginTransaction();
			sessao.save(edicao);
			
			transaction.commit();
			sessao.close();
			
			return true;
			
		} catch(Exception e){
			System.out.println(e.getMessage());
			return false;
		}		
		
	}
	
	
	public static Collection<EdicaoTO>  consultar(int cod_livro){
		 
		Collection<EdicaoTO> list = new ArrayList<EdicaoTO>(); 
		try {
		    Session session = fabrica.openSession();     
		    
		    Query q = session.createQuery("from EdicaoTO e where e.livro = :codigo");
		    q.setInteger("codigo", cod_livro);
		    		    
		    list = q.list();  
		    session.flush();     
		    session.close(); 
			
		} catch(Exception e)
			{System.out.println(e.getMessage());
		}
		
		return list;
	}

}
